<template>
  <div>

    <vxe-table
      ref="nbtjTable"
      :data="nbtjTableData"
      :footer-method="nbtjMethod"
      :merge-footer-items="nbtjMergeFooterItems"
      stripe
      border
      align="center"
      show-footer>
      <vxe-column type="seq" width="100"/>
      <vxe-colgroup title="负荷运行过程">
        <vxe-column field="ffuhe" title="负荷"/>
        <vxe-column field="fpower" title="功率(KW)">
          <template #default="{ row }">
            <span>{{ Math.ceil(nbtjCountFpower(row)) }} </span>
          </template>
        </vxe-column>
        <vxe-colgroup title="升降时间(分钟)">
          <vxe-column field="fsjtime" width="60" />
          <vxe-column field="fsjtime1" width="60" />
        </vxe-colgroup>
        <vxe-column field="fyxtime" title="运行时间(分钟)" width="60"/>
        <vxe-column field="fyh" title="对应油耗率(g/KWh)"/>
        <vxe-column field="fmaxyh" title="最大油耗率(g/KWh)">
          <template #default="{ row }">
            <span>{{ nbtjCountFmaxyh(row).toFixed(2) }} </span>
          </template>
        </vxe-column>
        <vxe-column field="fqigangyh" title="气缸油耗率（g/kWh）"/>
        <vxe-column field="fyryxh" title="引燃油耗率（g/kWh）"/>
        <vxe-column field="fnaoh" title="NaOH消耗率（l/h）"/>
        <vxe-column :edit-render="{}" field="fryxhl" title="燃油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fmaxyh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fqgyxhl" title="气缸油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fqigangyh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fyryxhl" title="引燃油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fyryxh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fnaohxhl" title="NaOH消耗量(l)">
          <!-- <template #default="{ row }">
            <span>{{ naoXhlCal(row, 'fnaoh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
      </vxe-colgroup>
    </vxe-table>

    <vxe-table
      ref="fhsj2Table"
      :data="fhsj2TableData"
      :footer-method="fhsj2Method"
      :merge-footer-items="fhsj2MergeFooterItems"
      stripe
      border
      align="center"
      show-footer>
      <vxe-column type="seq" width="100"/>
      <vxe-colgroup title="负荷升降过程估算">
        <vxe-column field="ffuhe" title="负荷"/>
        <vxe-column field="fpower" title="功率(KW)"/>
        <vxe-colgroup title="升降时间(分钟)">
          <vxe-column field="fsjtime" width="60" />
          <vxe-column field="fsjtime1" width="60" />
        </vxe-colgroup>
        <vxe-column field="fyxtime" title="运行时间(分钟)" width="60"/>
        <vxe-column field="fyh" title="对应油耗率(g/KWh)"/>

        <vxe-column field="fmaxyh" title="最大油耗率(g/KWh)">
          <template #default="{ row }">
            <span>{{ fhsj2CountFmaxyh(row).toFixed(2) }} </span>
          </template>
        </vxe-column>
        <vxe-column field="fqigangyh" title="气缸油耗率（g/kWh）"/>
        <vxe-column field="fyryxh" title="引燃油耗率（g/kWh）"/>
        <vxe-column field="fnaoh" title="NaOH消耗率（l/h）"/>
        <vxe-column :edit-render="{}" field="fryxhl" title="燃油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fmaxyh', 'fsjtime1').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fqgyxhl" title="气缸油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fqigangyh', 'fsjtime1').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fyryxhl" title="引燃油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fyryxh', 'fsjtime1').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fnaohxhl" title="NaOH消耗量(l)">
          <!-- <template #default="{ row }">
            <span>{{ naoXhlCal(row, 'fnaoh', 'fsjtime1').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
      </vxe-colgroup>
    </vxe-table>

    <vxe-table
      ref="zyqTable"
      :data="zyqTableData"
      :footer-method="zyqMethod"
      :merge-footer-items="zyqMergeFooterItems"
      style="margin-bottom: 10px;"
      stripe
      border
      align="center"
      show-footer>
      <vxe-column type="seq" width="100"/>
      <vxe-colgroup title="增压器匹配">
        <vxe-column field="ffuhe" title="负荷"/>
        <vxe-column field="fpower" title="功率(KW)"/>
        <vxe-colgroup title="升降时间(分钟)">
          <vxe-column field="fsjtime" width="60" />
          <vxe-column field="fsjtime1" width="60" />
        </vxe-colgroup>
        <vxe-column field="fyxtime" title="运行时间(分钟)" width="60"/>
        <vxe-column field="fyh" title="对应油耗率(g/KWh)"/>

        <vxe-column field="fmaxyh" title="最大油耗率(g/KWh)">
          <template #default="{ row }">
            <span>{{ zyqFmaxyh(row).toFixed(2) }} </span>
          </template>
        </vxe-column>
        <vxe-column field="fqigangyh" title="气缸油耗率（g/kWh）"/>
        <vxe-column field="fyryxh" title="引燃油耗率（g/kWh）"/>
        <vxe-column field="fnaoh" title="NaOH消耗率（l/h）"/>
        <vxe-column :edit-render="{}" field="fryxhl" title="燃油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fmaxyh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fqgyxhl" title="气缸油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fqigangyh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fyryxhl" title="引燃油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fyryxh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fnaohxhl" title="NaOH消耗量(l)">
          <!-- <template #default="{ row }">
            <span>{{ naoXhlCal(row, 'fnaoh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
      </vxe-colgroup>
    </vxe-table>

    <vxe-table
      ref="qtTable"
      :data="qtTableData"
      :footer-method="qtMethod"
      :merge-footer-items="qtMergeFooterItems"
      :footer-cell-class-name="footerCellClassName"
      stripe
      border
      align="center"
      show-footer>
      <vxe-column type="seq" width="100"/>
      <vxe-colgroup title="其他运行（含故障/功能性试验/安保试验等）">
        <vxe-column field="ffuhe" title="负荷"/>
        <vxe-column field="fpower" title="功率(KW)">
          <template #default="{ row }">
            <span>{{ Math.ceil(qtCountFpower(row)) }} </span>
          </template>
        </vxe-column>
        <vxe-colgroup title="升降时间(分钟)">
          <vxe-column field="fsjtime" width="60" />
          <vxe-column field="fsjtime1" width="60" />
        </vxe-colgroup>
        <vxe-column field="fyxtime" title="运行时间(分钟)" width="60"/>
        <vxe-column field="fyh" title="对应油耗率(g/KWh)"/>

        <vxe-column field="fmaxyh" title="最大油耗率(g/KWh)">
          <template #default="{ row }">
            <span>{{ qtFmaxyh(row).toFixed(2) }} </span>
          </template>
        </vxe-column>
        <vxe-column field="fqigangyh" title="气缸油耗率（g/kWh）"/>
        <vxe-column field="fyryxh" title="引燃油耗率（g/kWh）"/>
        <vxe-column field="fnaoh" title="NaOH消耗率（l/h）"/>
        <vxe-column :edit-render="{}" field="fryxhl" title="燃油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fmaxyh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fqgyxhl" title="气缸油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fqigangyh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fyryxhl" title="引燃油消耗量(kg)">
          <!-- <template #default="{ row }">
            <span>{{ xhlCal(row, 'fyryxh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
        <vxe-column field="fnaohxhl" title="NaOH消耗量(l)">
          <!-- <template #default="{ row }">
            <span>{{ naoXhlCal(row, 'fnaoh').toFixed(2) }} </span>
          </template> -->
        </vxe-column>
      </vxe-colgroup>
    </vxe-table>

  </div>
</template>
<script>
const FRepKey = 29015
import { jiachunList } from '../../api/rygl'
import { common } from '../../mixins/common'
import '../../styles/vxetable.scss'
export default {
  mixins: [common],
  data() {
    return {
      mergeFooterItems: [{ row: 0, col: 1, rowspan: 1, colspan: 6 }],
      fhsjMergeFooterItems: [
        { row: 0, col: 1, rowspan: 1, colspan: 6 },
        { row: 1, col: 1, rowspan: 1, colspan: 6 }
      ],
      fhsjTableData: [],

      nbtjMergeFooterItems: [{ row: 0, col: 1, rowspan: 1, colspan: 6 }],
      nbtjTableData: [],

      fhsj2MergeFooterItems: [
        { row: 0, col: 1, rowspan: 1, colspan: 6 },
        { row: 1, col: 1, rowspan: 1, colspan: 6 }
      ],
      fhsj2TableData: [],

      zyqMergeFooterItems: [{ row: 0, col: 1, rowspan: 1, colspan: 6 }],
      zyqTableData: [],

      qtMergeFooterItems: [
        { row: 0, col: 1, rowspan: 1, colspan: 6 },
        { row: 1, col: 1, rowspan: 1, colspan: 6 },
        { row: 2, col: 1, rowspan: 1, colspan: 6 }
      ],
      qtTableData: []
    }
  },
  async created() {
    await this.getRanyouList()
  },
  methods: {
    async getRanyouList() {
      try {
        const res = await jiachunList({
          opername: decodeURIComponent(this.globalDb.fname),
          fobjectid: FRepKey
        })
        // 29002  常规主燃油
        // 磨合调整过程  -- 负荷运行  fmhfhyx
        // 负荷升降估算  fmhfhsj

        // 内部提交和FAT过程  fnbfhyx
        // 负荷升降估算  fnbfhsj

        // 增压器匹配和性能调整  fzyqxn
        // 其他运行（含故障/功能性试验/安保试验等）  fqtyx
        // console.log(res.Data, ' res getRanyouList')
        if (res.Error === 0) {
          this.nbtjTableData = res.Data.ffuheyx // 负荷运行过程
          this.fhsj2TableData = res.Data.ffuhesj // 负荷升降过程估算
          this.zyqTableData = res.Data.fzyqxn // 增压器匹配+性能调试
          this.qtTableData = res.Data.fqtyx // 其他运行（含故障/功能性试验/安保试验等）
        }
      } catch (error) {
        console.log(error)
      }
    },
    /* 负荷运行过程开始************/
    nbtjCountFpower(row) {
      return Number(row.fpower)
    },
    nbtjCountFmaxyh(row) {
      if (row.ffuhe === '25%' || row.ffuhe === '50%') {
        return row.fyh * (1 + 0.07)
      } else if (row.ffuhe === '75%') {
        return row.fyh * (1 + 0.06)
      } else {
        return (row.fyh * (1 + 0.05))
      }
    },
    xhlCal(row, field, time = 'fyxtime') {
      // 燃气消耗量（kg）    = 最大气号率 * 功率(kW) * ( 运行时间 / 60) * 0.001
      // 气缸油消耗量        = 气缸油耗率 * 功率(kW) * ( 运行时间 / 60) * 0.001
      // 引燃油消耗量        = 引燃油耗率 * 功率(kW) * ( 运行时间 / 60) * 0.001
      if (time === 'fsjtime') {
        console.log(field, ' field', row)
      }
      return (Number(row[field]) * Number(row.fpower) * (Number(row[time]) / 60) * 0.001)
    },
    naoXhlCal(row, field, time = 'fyxtime') {
      // NaOH消耗量         = ( 运行时间 / 60) * NaOH消耗率
      return (Number(row[time]) / 60) * Number(row[field])
    },

    // 燃油消耗量(kg)
    fryxhlCal(row) {
      return Number(row.fryxhl)
    },
    // 气缸油消耗量(kg)
    fqgyxhlCal(row) {
      return Number(row.fqgyxhl)
    },
    // 引燃油消耗量(kg)
    fyryxhlCal(row) {
      return Number(row.fyryxhl)
    },
    // NaOH消耗量(l)
    fnaohxhlCal(row) {
      return Number(row.fnaohxhl)
    },
    xhlCalAll: function() {
      let count1 = 0
      let count2 = 0
      let count3 = 0
      let count4 = 0
      this.nbtjTableData.forEach(row => {
        // count1 += parseFloat(this.xhlCal(row, 'fmaxyh', 'fyxtime'))
        // count2 += parseFloat(this.xhlCal(row, 'fqigangyh', 'fyxtime'))
        // count3 += parseFloat(this.xhlCal(row, 'fyryxh', 'fyxtime'))
        // count4 += parseFloat(this.naoXhlCal(row, 'fnaoh', 'fyxtime'))

        count1 += parseFloat(this.fryxhlCal(row))
        count2 += parseFloat(this.fqgyxhlCal(row))
        count3 += parseFloat(this.fyryxhlCal(row))
        count4 += parseFloat(this.fnaohxhlCal(row))
      })

      let count5 = 0
      let count6 = 0
      let count7 = 0
      let count8 = 0
      this.fhsj2TableData.forEach(row => {
        // count5 += parseFloat(this.xhlCal(row, 'fmaxyh', 'fsjtime1'))
        // count6 += parseFloat(this.xhlCal(row, 'fqigangyh', 'fsjtime1'))
        // count7 += parseFloat(this.xhlCal(row, 'fyryxh', 'fsjtime1'))
        // count8 += parseFloat(this.naoXhlCal(row, 'fnaoh', 'fsjtime1'))

        count5 += parseFloat(this.fryxhlCal(row))
        count6 += parseFloat(this.fqgyxhlCal(row))
        count7 += parseFloat(this.fyryxhlCal(row))
        count8 += parseFloat(this.fnaohxhlCal(row))
      })

      let count9 = 0
      let count10 = 0
      let count11 = 0
      let count12 = 0
      this.zyqTableData.forEach(row => {
        // count9 += parseFloat(this.xhlCal(row, 'fmaxyh', 'fyxtime'))
        // count10 += parseFloat(this.xhlCal(row, 'fqigangyh', 'fyxtime'))
        // count11 += parseFloat(this.xhlCal(row, 'fyryxh', 'fyxtime'))
        // count12 += parseFloat(this.naoXhlCal(row, 'fnaoh', 'fyxtime'))

        count9 += parseFloat(this.fryxhlCal(row))
        count10 += parseFloat(this.fqgyxhlCal(row))
        count11 += parseFloat(this.fyryxhlCal(row))
        count12 += parseFloat(this.fnaohxhlCal(row))
      })

      let count13 = 0
      let count14 = 0
      let count15 = 0
      let count16 = 0
      this.qtTableData.forEach(row => {
        // count13 += parseFloat(this.xhlCal(row, 'fmaxyh', 'fyxtime'))
        // count14 += parseFloat(this.xhlCal(row, 'fqigangyh', 'fyxtime'))
        // count15 += parseFloat(this.xhlCal(row, 'fyryxh', 'fyxtime'))
        // count16 += parseFloat(this.naoXhlCal(row, 'fnaoh', 'fyxtime'))

        count13 += parseFloat(this.fryxhlCal(row))
        count14 += parseFloat(this.fqgyxhlCal(row))
        count15 += parseFloat(this.fyryxhlCal(row))
        count16 += parseFloat(this.fnaohxhlCal(row))
      })
      return {
        count1: Number(count1), count2: Number(count2), count3: Number(count3), count4: Number(count4),
        count5: Number(count5), count6: Number(count6), count7: Number(count7), count8: Number(count8),
        count9: Number(count9), count10: Number(count10), count11: Number(count11), count12: Number(count12),
        count13: Number(count13), count14: Number(count14), count15: Number(count15), count16: Number(count16)
      }
    },
    // 表尾数据
    nbtjMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 1) {
            return '合计消耗量（kg）'
          }
          if (columnIndex === 11) {
            return ` ${this.xhlCalAll().count1.toFixed(2)} `
          }
          if (columnIndex === 12) {
            return ` ${this.xhlCalAll().count2.toFixed(2)} `
          }
          if (columnIndex === 13) {
            return ` ${this.xhlCalAll().count3.toFixed(2)} `
          }
          if (columnIndex === 14) {
            return ` ${this.xhlCalAll().count4.toFixed(2)} `
          }
          return ''
        })
      ]
    },
    /* 负荷运行过程结束************/
    fhsj2CountFmaxyh(row) {
      return row.fyh * (1 + 0.07)
    },
    // 表尾数据
    fhsj2Method({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 1) {
            return '合计消耗量（kg）'
          }
          if (columnIndex === 11) {
            return ` ${this.xhlCalAll().count5.toFixed(2)} `
          }
          if (columnIndex === 12) {
            return ` ${this.xhlCalAll().count6.toFixed(2)} `
          }
          if (columnIndex === 13) {
            return ` ${this.xhlCalAll().count7.toFixed(2)} `
          }
          if (columnIndex === 14) {
            return ` ${this.xhlCalAll().count8.toFixed(2)} `
          }
          return ''
        }),
        columns.map((column, columnIndex) => {
          if (columnIndex === 1) {
            return '内部提交和FAT总消耗量（kg）'
          }
          const {
            count1, count2, count3, count4,
            count5, count6, count7, count8 } = this.xhlCalAll()
          if (columnIndex === 11) {
            return ` ${(2 * (count1 + count5)).toFixed(2)} `
          }
          if (columnIndex === 12) {
            return ` ${(2 * (count2 + count6)).toFixed(2)} `
          }
          if (columnIndex === 13) {
            return ` ${(2 * (count3 + count7)).toFixed(2)} `
          }
          if (columnIndex === 14) {
            return ` ${(2 * (count4 + count8)).toFixed(2)} `
          }
          return ''
        })
      ]
    },

    zyqCountAmount(row) {
      return (Number(row.fpower) * Number(row.fyh) * (Number(row.fyxtime) / 60) * 0.001)
    },
    zyqCountAllAmount: function() {
      let count = 0
      this.zyqTableData.forEach(row => {
        count += parseFloat(this.zyqCountAmount(row))
      })
      return count
    },
    // 表尾数据
    zyqMethod({ columns, data }) {
      return []
    },
    zyqFmaxyh(row) {
      return (row.fyh * (1 + 0.05))
    },

    qtCountFpower(row) {
      return Number(row.fpower)
    },
    qtCountAmount(row) {
      return (Number(row.fpower) * Number(row.fyh) * (Number(row.fyxtime) / 60) * 0.001)
    },
    qtCountAllAmount: function() {
      let count = 0
      this.qtTableData.forEach(row => {
        count += parseFloat(this.qtCountAmount(row))
      })
      return count
    },
    footerCellClassName({ $rowIndex, columnIndex }) {
      if (columnIndex === 9 || columnIndex === 10 || columnIndex === 11 || columnIndex === 12) {
        if ($rowIndex === 0) {
          return 'col-blue'
        }
      }
      if (columnIndex === 9 || columnIndex === 10 || columnIndex === 11 || columnIndex === 12) {
        if ($rowIndex === 1) {
          return 'col-red'
        }
      }
    },
    // 表尾数据
    qtMethod({ columns, data }) {
      return [
        // 后续机燃油消耗总量（kg）
        columns.map((column, columnIndex) => {
          if (columnIndex === 1) {
            return '首台机总消耗量（kg）'
          }
          const {
            count1, count2, count3, count4,
            count5, count6, count7, count8,
            count9, count10, count11, count12,
            count13, count14, count15, count16
          } = this.xhlCalAll()
          if (columnIndex === 11) {
            return ` ${
              (2 * (count1 + count5) +
              count9 +
              count13).toFixed(2)
            } `
          }
          if (columnIndex === 12) {
            return ` ${
              (2 * (count2 + count6) +
              count10 +
              count14).toFixed(2)
            } `
          }
          if (columnIndex === 13) {
            return ` ${
              (2 * (count3 + count7) +
              count11 +
              count15).toFixed(2)
            } `
          }
          if (columnIndex === 14) {
            return ` ${
              (2 * (count4 + count8) +
              count12 +
              count16).toFixed(2)
            } `
          }
          return ''
        }),
        columns.map((column, columnIndex) => {
          if (columnIndex === 1) {
            return '后续机总消耗量（kg）'
          }
          const {
            count1, count2, count3, count4,
            count5, count6, count7, count8,
            // count9, count10, count11, count12,
            count13, count14, count15, count16
          } = this.xhlCalAll()
          if (columnIndex === 11) {
            return ` ${
              (2 * (count1 + count5) +
              count13).toFixed(2)
            } `
          }
          if (columnIndex === 12) {
            return ` ${
              (2 * (count2 + count6) +
              count14).toFixed(2)
            } `
          }
          if (columnIndex === 13) {
            return ` ${
              (2 * (count3 + count7) +
              count15).toFixed(2)
            } `
          }
          if (columnIndex === 14) {
            return ` ${
              (2 * (count4 + count8) +
              count16).toFixed(2)
            } `
          }
          return ''
        })
      ]
    },
    qtFmaxyh(row) {
      return (row.fyh * (1 + 0.05))
    }
  }
}
</script>
<style scoped lang="scss">
  .input {
    width: 10%;
    margin-top: 10px;
    margin-bottom: 30px;
  }

</style>
